#if UNITY_EDITOR
using System;
using System.Linq;
using System.Collections.Generic;
using UnityEditor.IMGUI.Controls;
using UnityEngine;
using UnityEditor;

namespace GameDesigner
{
    public class AddComponentWindow : AdvancedDropdown
    {
        private readonly string rootName;
        private readonly List<Type> types;
        private Action<Type> onSelect;

        public AddComponentWindow(string rootName, List<Type> types, AdvancedDropdownState state) : base(state)
        {
            this.rootName = rootName;
            this.types = types;
        }

        public void Init(Rect rect, Action<Type> onSelect)
        {
            this.onSelect = onSelect;
            Show(rect);
        }

        protected override AdvancedDropdownItem BuildRoot()
        {
            var root = new AdvancedDropdownItem(rootName);
            var dictionary = new Dictionary<string, int>();
            foreach (var type in types)
            {
                var typeName = type.ToString();
                var array = typeName.Split('.', StringSplitOptions.RemoveEmptyEntries);
                var advancedDropdownItem2 = root;
                for (int j = 0; j < array.Length; j++)
                {
                    var text = array[j];
                    if (j == array.Length - 1)
                    {
                        var componentDropdownItem = new ComponentDropdownItem(text, typeName, type)
                        {
                            icon = EditorGUIUtility.IconContent("d_cs Script Icon").image as Texture2D
                        };
                        advancedDropdownItem2.AddChild(componentDropdownItem);
                    }
                    else
                    {
                        if (!dictionary.TryGetValue(text, out var pathHashCode))
                        {
                            pathHashCode = text.GetHashCode();
                            dictionary[text] = pathHashCode;
                        }
                        var componentDropdownItem2 = Enumerable.SingleOrDefault(advancedDropdownItem2.children, (AdvancedDropdownItem c) => c.id == pathHashCode);
                        if (componentDropdownItem2 == null)
                        {
                            componentDropdownItem2 = new ComponentDropdownItem(text, typeName, type);
                            advancedDropdownItem2.AddChild(componentDropdownItem2);
                        }
                        advancedDropdownItem2 = componentDropdownItem2;
                    }
                }
            }
            var componentDropdownItem3 = new AdvancedDropdownItem("New script");
            componentDropdownItem3.AddChild(new NewScriptDropdownItem());
            root.AddChild(componentDropdownItem3);
            return root;
        }

        protected override void ItemSelected(AdvancedDropdownItem item)
        {
            if (item is ComponentDropdownItem componentItem)
            {
                onSelect?.Invoke(componentItem.type);
            }
            else if (item is NewScriptDropdownItem newScriptDropdownItem)
            {
                onSelect?.Invoke(null);
            }
        }
    }

    internal class ComponentDropdownItem : AdvancedDropdownItem
    {
        internal readonly string className;
        internal readonly Type type;

        public ComponentDropdownItem(string name, string className, Type type) : base(name)
        {
            this.className = className;
            this.type = type;
        }
    }

    internal class NewScriptDropdownItem : AdvancedDropdownItem
    {
        public NewScriptDropdownItem() : base("New Script")
        {
        }
    }
}
#endif